'use strict';
const db = uniCloud.database();

async function select_work(event) {
	//event为客户端上传的参数
	//console.log('event : ', event)
	let r = {
		err: 0
	}

	if (!event.work_card) {
		return r;
	}
	let res = await db.collection('work_user').where({
		"work_card": event.work_card
	}).get()

	//console.log(res)
	if (res.affectedDocs < 1) {
		return r;
	}
	let name_id = res.data[0].name_id;
	let info = await db.collection('user_info').doc(name_id).get();
	r = info.data[0].info
	r.err = 1
	//console.log(info.data[0].info)
	//返回数据给客户端
	return r
}

async function select_list(event) {
	let r = {}
	r.l = 0;
	r.li = [{
		"name": "",
		"subTitle": "",
		"icon": "",
		"path": ""
	}]
	
	// let article = await db.collection('work_user,user_info').get()
	
	// return article
	
	 let res = await db.collection('user_info').get();
	for (let i = 0; i < res.affectedDocs; i++) {
		let t = res.data[i].info;
		r.li[i] = {
			"name": res.data[i].info.full_name + "[" + t.Position + "]",
			"subTitle": t.Student_number,
			"icon": t.inonPath,
			"path": ""
		}
	}
	r.l = res.affectedDocs;
	return r;
}



exports.main = async (event, context) => {
	let r = {
		err: 0
	}
	console.log('event : ', event)
	if (!event.type) {
		return event;
	}

	switch (event.type) {
		case "select_work":
			return select_work(event);
			break;
		case "select_list":
			return select_list(event);
			break;
	}
	return r
};
